package lc;

import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;

/**
 * 证明类加载器只能加载jar包，不能加载外部单独配置文件
 */
public class Property {

    public static void main(String[] args) throws MalformedURLException, ClassNotFoundException, IllegalAccessException, InstantiationException {
//        String rootDir = "/Users/sunyuming/Downloads/jars/MyTest-1.0-SNAPSHOT.jar";
//        File file = new File(rootDir);
//        URI uri=file.toURI();
//        URL[] urls={uri.toURL()};


        String dir = "file:/Users/sunyuming/Documents/tool/jars/MyTest-1.0-SNAPSHOT.jar";
        URL url = new URL(dir);
        String resource = "file:/Users/sunyuming/Documents/tool/jars/testresource";
        URL[] urls2 = {url, new URL(resource)};

        // 无法读到（正常）
        MyUrlClassLoader myUrlClassLoader = new MyUrlClassLoader(urls2);
        URL urlResource = myUrlClassLoader.getResource("testresource");
        System.out.println(urlResource);

    }


    private static class MyUrlClassLoader extends URLClassLoader {

        public MyUrlClassLoader(URL[] urls) {
            super(urls);
        }

        @Override
        protected Class<?> findClass(final String name) throws ClassNotFoundException {
            return super.findClass(name);
        }
    }
}